//
//  TicketSelectCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2018/12/4.
//  Copyright © 2018 zwb. All rights reserved.
//

import UIKit

class TicketSelectCell: BaseTableViewCell {
    
    lazy var  selBtn: UIButton = {
        let v = UIButton()
        v.isUserInteractionEnabled = false
        v.setImage(UIImage(named: "selectButton"), for: .normal)
        return v
    }()
    
    lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
        v.layer.masksToBounds = true
        return v
    }()
    lazy var labLeft: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 16)
        return v
    }()
    lazy var lab2: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 16)
        v.textColor = Theme.ORANGE
        return v
    }()
    lazy var labContent: UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var line: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "efefef")
        return v
    }()
    
    lazy var  btn1: CheckVoButton = {
        let v = CheckVoButton()
        
        return v
    }()
    lazy var btnPdf: PDFButton =  {
        let v = PDFButton()
        
        return v
    }()
    
    
    lazy var attributes:[NSAttributedString.Key : NSObject] = {
        let paraph = NSMutableParagraphStyle()
        //将行间距设置为
        paraph.lineSpacing = 4
        //样式属性集合
        let titleAttributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                               NSAttributedString.Key.paragraphStyle: paraph]
        return titleAttributes
    }()
    var one = TicketModel(json: "") {
        didSet {
            labLeft.text = "\(one.fInvoicetypeNm.unwrap)"
            lab2.text = "￥\(one.fTotalamount.unwrap)"
            
            var combineStr = ""
            if one.fInvoicedate.unwrap != "" {
                combineStr += "开票日期        \(one.fInvoicedate.unwrap)"
            }
            if one.fInvoiceno.unwrap != "" {
                combineStr += "\n发票号码        \(one.fInvoiceno.unwrap)"
            }
            if one.fBuyername.unwrap != "" {
                combineStr += "\n购买方            \(one.fBuyername.unwrap)"
            }
            if one.fLabel.unwrap != "" {
                combineStr += "\n标签                \(one.fLabel.unwrap)"
            }
            if one.withSource {
                var danjuStr = ""
                if one.fCompany.intWrap == 1 {
                    danjuStr  = "\(one.sNo.unwrap)(\(one.vouType.unwrap))"
                } else {
                    danjuStr  = "\(one.inputNum.unwrap)(\(one.vouType.unwrap))"
                }
                if danjuStr != "()" {
                    combineStr += "\n所属单据        \(danjuStr)"
                }
                
                
            }
            
            labContent.attributedText = NSAttributedString(string: combineStr, attributes: attributes)
           
            
 
            btn1.setCheckStatus(one: one)
            
    
            
            if ifNeedPDF(one: one) {
                btnPdf.isHidden = false
                btnPdf.setHasPDF(flag: one.hasPdf.isSome)
            } else {
                btnPdf.isHidden = true
            }
            
            
            
            selBtn.isHidden = false
            
            self.labLeft.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(45)
            }
            self.labContent.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(45)
            }
            self.selBtn.snp.remakeConstraints { make in
                make.width.equalTo(30)
                make.height.equalTo(30)
                make.left.equalToSuperview().offset(10)
                make.centerY.equalToSuperview()
            }
            
            
            self.bg.backgroundColor = one.selectable ? .white : #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
            
            if one.isselected == 0 {
                selBtn.setImage(UIImage(named:"selectButton"), for: .normal)
            } else {
                selBtn.setImage(UIImage(named:"selectedButton"), for: .normal)
            }
            
            
            
            
            
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        
        contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6)
            make.top.equalTo(contentView).offset(6)
            make.right.equalToSuperview().offset(-6)
            make.bottom.equalTo(contentView)
        }
        bg.addSubview(labLeft)
        bg.addSubview(lab2)
        bg.addSubview(labContent)
        bg.addSubview(line)
        bg.addSubview(btn1)
        bg.addSubview(btnPdf)
        bg.addSubview(selBtn)
        selBtn.snp.makeConstraints { make in
            make.width.equalTo(30)
            make.height.equalTo(30)
            make.left.equalToSuperview().offset(-30)
            make.centerY.equalToSuperview()
        }
        labLeft.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.top.equalTo(bg).offset(8)
        }
        lab2.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.top.equalToSuperview().offset(8)
        }
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(1)
            make.top.equalTo(labLeft.snp.bottom).offset(3)
            
        }
        labContent.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-40)
        }
        
        btn1.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(27)
            make.width.equalTo(70)
            make.bottom.equalToSuperview().offset(-6)
        }
        btnPdf.snp.makeConstraints { make in
            make.right.equalTo(btn1.snp.left).offset(-8)
            make.height.equalTo(27)
            make.width.equalTo(70)
            make.centerY.equalTo(btn1)
        }
    
        
      
        bg.bringSubviewToFront(selBtn)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
